package com.hxm.appGenealogy.service;

import com.alibaba.fastjson.JSON;
import com.hxm.appGenealogy.Function.DynamicFunction;
import com.hxm.appGenealogy.Function.FamilyFunction;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.*;
import com.hxm.appGenealogy.mapper.PeopleMapper;
import com.hxm.appGenealogy.mapper.imagePhMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW)
public class FamliyService {

    @Autowired
    private FamilyFunction familyFunction;

    @Autowired
    private PeopleFunction peopleFunction;

    @Autowired
    private DynamicFunction dynamicFunction;

    @Autowired
    private PeopleMapper peopleMapper;


    @Autowired
    private imagePhMapper imageMapper;

    public ResponDataVo selectByid(Long id) {
        return familyFunction.selectByid(id);

    }

    public ResponDataVo updateById(Family family) {
        return familyFunction.updateById(family);
    }

    public ResponDataVo selectGetListQuery(String query) {
        return familyFunction.selectGetListQuery(query);
    }

    public ResponDataVo selectGetOneQuery(String query) {
        return familyFunction.selectGetOneQuery(query);
    }


    /**
     * 添加家族;
     *
     * @param family
     * @return
     */
    public ResponDataVo insert(Family family) {
        String parmAnalysis = ParmIntended.parmAnalysis(family, "creatorPeopleId");
        if (parmAnalysis != null) {
            return new ResponDataVo("缺少参数" + parmAnalysis);
        }
        QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
        queryWrapperVO.eq("id", family.getCreatorPeopleId());
        People people = peopleFunction.queryGetOneString(JSON.toJSONString(queryWrapperVO));
        if (people == null) {
            return new ResponDataVo("没有当前人的信息;");
        }
        QueryWrapperVO queryWrapperVO1 = new QueryWrapperVO();
        queryWrapperVO1.eq("id", family.getCreatorPeopleId());
        People people1 = peopleFunction.queryGetOneString(JSON.toJSONString(queryWrapperVO1));
        if (people1.getFamilyId() != null) {
            return new ResponDataVo("您已经有一个家族了;");
        }
        //设置创建家族的时间;
        family.setTime(new Date());
        family.setUsableSpace(1024.00);
        family.setUseSpace(0.00);
        familyFunction.insert(family);
        //设置权限为创始人;
        people.setJurisdiction(0);
        people.setIsHavePit(0);
        people.setFamilyId(family.getId());
        peopleFunction.updateByid(people);
        //为当前的家族创建一个相册;
        imagePh imagePh = new imagePh();
        imagePh.setQuantity(0);
        imagePh.setFanilyId(family.getId());
        imagePh.setPeopleId(family.getCreatorPeopleId());
        imagePh.setPhotoCover(null);
        imagePh.setTime(new Date());
        imagePh.setPhotoName("默认相册");
        imageMapper.insert(imagePh);
        return new ResponDataVo(family);
    }


    /**
     * 判断是否有家族;
     * @param people
     * @return
     */
    public ResponDataVo idHaveFamliy(People people) {
        //0是没有 1是有;
        Map<String, Object> objectMap = new HashMap<>(1);
        String parmAnalysis = ParmIntended.parmAnalysis(people, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
        queryWrapperVO.eq("id", people.getId());
        People queryGetOneString = peopleFunction.queryGetOneString(JSON.toJSONString(queryWrapperVO));
        if (queryGetOneString.getFamilyId() != null) {
            objectMap.put("ishave", 1);
        } else {
            objectMap.put("ishave", 0);
        }
        return new ResponDataVo(objectMap);
    }

    /**
     * 加入家族;
     *
     * @param familyVo
     * @return
     */
    public ResponDataVo joinFamliy(@RequestBody FamilyVo familyVo) {
        String parmAnalysis = ParmIntended.parmAnalysis(familyVo, "peopleId", "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //判断家族是否存在;
        Family family = (Family) familyFunction.selectByid(familyVo.getId()).getData();
        if (family == null) {
            return new ResponDataVo("没有当前家族的信息");
        }
        if (family.getCreatorPeopleId().equals(familyVo.getPeopleId())) {
            return new ResponDataVo("您已经加入到您自己创建的家族;");
        }
        //判断要加入家族的人是否已经加入过了家族;
        People people = peopleFunction.peopleGetByid(familyVo.getPeopleId());
        if (people == null) {
            return new ResponDataVo("没有当前要加入家族人的信息;");
        }
        if (people.getFamilyId() != null) {
            return new ResponDataVo("当前要加入家族的人已经有了家族;");
        }
        people.setFamilyId(familyVo.getId());
        //目前不在树里面;
        people.setIsHavePit(3);
        //普通成员;
        people.setJurisdiction(2);
        people.setFamilyMergeId(family.getId());
        List<Dynamic> dynamicList = dynamicFunction.selectGetListQuery(JSON.toJSONString(new QueryWrapperVO().eq("people_id", people.getId())));
        if (dynamicList.size() > 0) {
            for (Dynamic dynamic : dynamicList) {
                dynamic.setFamilyId(familyVo.getId());
                dynamicFunction.updateByid(dynamic);
            }
        }
        peopleFunction.updateByid(people);
        return new ResponDataVo(true);
    }


    /**
     * 查询家族的成员的信息;
     *
     * @param family
     * @return
     */
    public ResponDataVo selectFamliyPeople(Family family) {
        String parmAnalysis = ParmIntended.parmAnalysis(family, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
        queryWrapperVO.eq("family_id", family.getId());
        queryWrapperVO.isNull("family_merge_id");
        List<People> people = peopleFunction.queryGeListString(JSON.toJSONString(queryWrapperVO));
        return new ResponDataVo(people);
    }

    /**
     * 扫码补坑;
     *
     * @param peopleVo qrPopleId 补坑人;
     *                 id 站坑人;
     * @return
     */
    public ResponDataVo qrOit(PeopleVo peopleVo) {
        //参数教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, "qrPopleId", "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //站坑的人;
        People qrPeople = peopleFunction.peopleGetByid(peopleVo.getId());
        if (qrPeople == null) {
            return new ResponDataVo("没有扫描的人员的信息;");
        }
        //补坑的人;
        People people1 = peopleFunction.peopleGetByid(peopleVo.getQrPopleId());
        if (people1 == null) {
            return new ResponDataVo("请确认是否存在当前的坑位;");
        }

        if (people1.getNumber() != null) {
            //手机号
            qrPeople.setNumber(people1.getNumber());
            people1.setNumber(null);
        }  if (people1.getAvater() != null) {
            //头像
            qrPeople.setAvater(people1.getAvater());
            //people1.setNumber(null);
        }
        if (people1.getName() != null) {
            //名
            qrPeople.setName(people1.getName());
            //people1.setName(null);
        }
        if (people1.getIdCard() != null) {
            //身份证;
            qrPeople.setIdCard(people1.getIdCard());
            //people1.setIdCard(null);
        }
        if (people1.getAncestral() != null) {
            //祖籍;
            qrPeople.setAncestral(people1.getAncestral());
        }
        if (people1.getOpenId() != null) {
            //微信用户的唯一的标识;
            qrPeople.setOpenId(people1.getOpenId());
        }
        if (people1.getSurname() != null) {
            //姓;
            qrPeople.setSurname(people1.getSurname());
        }
        if (people1.getPosition() != null) {
            //职位;
            qrPeople.setPosition(people1.getPosition());
        }
        if (people1.getBrief() != null) {
            qrPeople.setBrief(people1.getBrief());
        }
        if (people1.getBirthdayTime() != null) {
            qrPeople.setBirthdayTime(people1.getBirthdayTime());
        }
        if (people1.getEducation() != null) {
            qrPeople.setEducation(people1.getEducation());
        }
        if (people1.getWorkUnit() != null) {
            qrPeople.setWorkUnit(people1.getWorkUnit());
        }
        //设置当前的人为在树的状态,应该当前补坑的人的信息关系已经删除;
        people1.setIsHavePit(1);
        //peopleFunction.updateByid(people1);
      /*  peopleMapper.ipdateMsg(people1);*/
        peopleFunction.delPeopleByid(people1.getId());
        //已经在数;
        qrPeople.setIsHavePit(0);
        peopleFunction.updateByid(qrPeople);
        return new ResponDataVo(people1);
    }

    /**
     * 添加管理员权限;
     * @param familyVo
     * @return
     */
    public ResponDataVo addJurisdiction(FamilyVo familyVo) {
        //参数教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(familyVo, "peopleId", "id", "creatorPeopleId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        Family family = (Family) familyFunction.selectByid(familyVo.getId()).getData();
        if (family == null) {
            return new ResponDataVo("不存在当前的家族;");
        }
        if (!family.getCreatorPeopleId().equals(familyVo.getCreatorPeopleId())) {
            return new ResponDataVo("您没有这个权限,只有家族的创建者,才可以添加权限;");
        }
        People people = peopleFunction.peopleGetByid(familyVo.getPeopleId());
        if (people == null) {
            return new ResponDataVo("不存在被添加权限者;");
        }
        if (!people.getFamilyId().equals(familyVo.getId())) {
            return new ResponDataVo("您添加的成员不是本族的成员,请添加本族的成员;");
        }
        people.setJurisdiction(1);
        peopleFunction.updateByid(people);
        return new ResponDataVo(people);
    }

    public ResponDataVo cancelJurisdiction(@RequestBody FamilyVo familyVo) {
        //参数教验证;
        String parmAnalysis = ParmIntended.parmAnalysis(familyVo, "peopleId", "id", "creatorPeopleId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        Family family = (Family) familyFunction.selectByid(familyVo.getId()).getData();
        if (family == null) {
            return new ResponDataVo("不存在当前的家族;");
        }
        if (!family.getCreatorPeopleId().equals(familyVo.getCreatorPeopleId())) {
            return new ResponDataVo("您没有这个权限,只有家族的创建者,才可以取消管理员的权限;");
        }
        People people = peopleFunction.peopleGetByid(familyVo.getPeopleId());
        if(people.getJurisdiction()==2){
            return new ResponDataVo("当前的人是普通的家族成员,属于最低的权限,不可以在次的进行限制;");
        }
        //当前的人设置为普通的人的信息,不是当前的管理人员的信息;
        people.setJurisdiction(2);
        peopleFunction.updateByid(people);
        return new ResponDataVo(true);
    }

    /**
     * 获取可用的照片的大小;
     * @param family
     * @return
     */
    public ResponDataVo imageSize(Family family) {
        String parmAnalysis = ParmIntended.parmAnalysis(family, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        Family familys = (Family) familyFunction.selectByid(family.getId()).getData();
        Map<String, Object> map = new HashMap<>();
        //已将使用了的空间;
        map.put("useSpace", familys.getUseSpace());
        //剩余的空间;
        map.put("usableSpace", familys.getUsableSpace());
        return new ResponDataVo(map);
    }


}
